package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.TContacts;
import com.ldatum.crm.domain.TCustomer;
import com.ldatum.crm.service.TContactsService;
import com.ldatum.crm.service.TCustomerService;
import com.ldatum.crm.serviceImpl.TContactsServiceImpl;
import com.ldatum.crm.serviceImpl.TCustomerServiceImpl;

@ParentPackage("admin")

public class ContactsAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TContacts contacts ;
	
	private List<TContacts> contactsList = new ArrayList<TContacts>(); 
	private List<TCustomer> customerList = new ArrayList<TCustomer>();
	
	TContactsService contactsService = new TContactsServiceImpl();
	TCustomerService customerService = new TCustomerServiceImpl();
	
	private int contactsId;
	private int customerId;
	private String ids;
	private String customerName;
	
	private String levelOne;
	private String levelTwo;
	
	public void findById(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TContacts contacts = contactsService.findById(TContacts.class, contactsId);
			json.accumulate("contacts", contacts);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("contacts", null);
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public void findByCustomerId(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TCustomer cus = customerService.findById(TCustomer.class, customerId);
			json.accumulate("contactsList", contactsService.findByCustomerId(customerId));
			json.accumulate("currentStep", cus.getCurrentStep());
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public void findByCustomer(){ 
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			contactsList = contactsService.findByCustomerId(customerId);
			for(TContacts c:contactsList){
				c.setCustomer(null);
			}
			json.accumulate("contactsList", contactsList);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	
	public String list() throws Exception{
		contactsList = contactsService.list(TContacts.class,(String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME));
		levelOne = "customer";
		levelTwo = "contacts";
		return "listContacts";
		
	}
	
	public String edit() throws Exception{
		contacts = contactsService.queryById(TContacts.class, contactsId);
		levelOne = "customer";
		levelTwo = "contacts";
		return "editContacts";
	}
	
	public String add() throws Exception{
		customerList = customerService.myCustomer(TCustomer.class,(String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME));
		levelOne = "customer";
		levelTwo = "contacts";
		return "addContacts";
	}
	
	public void update() throws Exception{
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TCustomer customer = new TCustomer();
			customer.setId(customerId);
			contacts.setCustomer(customer);
			contactsService.update(contacts, contactsId);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public void save() throws Exception{
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TCustomer customer = new TCustomer();
			customer.setId(customerId);
			contacts.setCustomer(customer);
			contactsService.add(contacts);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	/**
	 * 可批量删除选中的联系人信息
	 */
	public void deletes(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			contactsService.deletes(ids);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	
	
	public String getIds() {
		return ids;
	}


	public void setIds(String ids) {
		this.ids = ids;
	}


	public TContacts getContacts() {
		return contacts;
	}

	public void setContacts(TContacts contacts) {
		this.contacts = contacts;
	}

	public int getContactsId() {
		return contactsId;
	}

	public void setContactsId(int contactsId) {
		this.contactsId = contactsId;
	}

	public List<TContacts> getContactsList() {
		return contactsList;
	}

	public void setContactsList(List<TContacts> contactsList) {
		this.contactsList = contactsList;
	}


	public int getCustomerId() {
		return customerId;
	}


	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}


	public List<TCustomer> getCustomerList() {
		return customerList;
	}


	public void setCustomerList(List<TCustomer> customerList) {
		this.customerList = customerList;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}
}

